﻿using System;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using DataAccess.SystemManage;
using Ninject;
using WinForm.Helper;
using WinForm.SystemManage;

namespace WinForm
{
    public partial class LoginForm : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ISysUser _sysUser = Kernel.Get<ISysUser>();

        public LoginForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //数据库验证
            string errorMessage = string.Empty;
            try
            {
                //密码使用md5加密
                string md5Password = ParseMD5String.GetMD5Password(txtPassword.Text);
                ResultInfo<jsh_user> u = _sysUser.Login(txtUserName.Text, md5Password);
                if (u.ResultNumber == ResultEnum.Success && u.Obj != null && u.Obj.id > 0)
                {
                    //systeminfo赋值
                    SystemInfo.OperateId = u.Obj.id;
                    SystemInfo.OperateName = u.Obj.displayname;
                    SystemInfo.OperateAddressId = 00000001;//临时编号

                    Visible = false;

                    AssetConfrim assetConfrim=new AssetConfrim();
                    assetConfrim.ShowDialog();

                    /*xiangzy 20140617注释，显示资产确认界面，然后由资产确认界面进入主界面
                    //显示主界面
                    var frmNew = new FrmNew { StartPosition = FormStartPosition.CenterScreen };

                    frmNew.ShowDialog();
                    */

                    ////设备没有初始化也可以进入系统，只是和设备相关的功能不能使用
                    ////初始化设备
                    //var zq58Logical = new Zq58Logical();
                    //ResultInfo<string> equip = zq58Logical.CheckEquipment();
                    //if (equip.ResultNumber == ResultEnum.Success)
                    //{
                    //    //保存设备信息
                    //    SystemInfo.OperatePsamId = equip.Obj;
                    //}
                    //else
                    //{
                    //    MyMessageBox.ShowMessageBoxInfo("设备初始化出现问题，请确认是否需要使用读卡设备。\n如果需要，请在系统中直接初始化设备。");
                    //}

                    Close();
                    Dispose();

                }
                else
                {
                    errorMessage = errorMessage + u.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }

            ////现在测试阶段直接进入
            ////显示主界面
            //this.Visible = false;
            //FrmNew frmNew = new FrmNew();
            //frmNew.StartPosition = FormStartPosition.CenterScreen;
            //frmNew.ShowDialog();
            //this.Close();
            //this.Dispose();
        }

        private void LoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}
